//
//  XHWaitViewController.m
//  xinhua
//
//  Created by fengyn on 15/11/4.
//  Copyright © 2015年 lexingsoft. All rights reserved.
//

#import "XHWaitViewController.h"
#import "AppDelegate.h"

#import "APIKey.h"
#import "Services.h"

#import <UIAlertView-Blocks/UIAlertView+Blocks.h>

@interface XHWaitViewController ()

@end

@implementation XHWaitViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
}

- (void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
//    [self checkAppVersion];
    [self autoLogin];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

- (void)backAction{
    AppDelegate *delegate=[UIApplication sharedApplication].delegate;
    [delegate finishLaunching];
}

//- (void)failureAction{
//    AppDelegate *delegate=[UIApplication sharedApplication].delegate;
//    [delegate goLoginAction];
//}

- (void)autoLogin{
    WS(weakSelf)
    if ([[DataManager sharedInstance] isLogin]) {
        /*
        [YunBaService setAlias:[DataManager sharedInstance].userId resultBlock:^(BOOL succ, NSError *error) {
            
        }];
         */
    }
    [ServiceManager autoLoginSuccess:^(NSDictionary *data) {
        [weakSelf backAction];
    } failure:^{
        [weakSelf backAction];
    }];
}

- (void)checkAppVersion{
    [ServiceManager checkIsNeedForceUpdateSuccess:^(NSDictionary *data) {
        [self autoLogin];
        if ([data isKindOfClass:[NSDictionary class]]) {
            [self autoLogin];
            NSString *appVersion=data[@"ios_version"];
            NSArray *versionArray=[appVersion componentsSeparatedByString:@"."];
            NSString *currentVersion=[TakeInfoPlist CFBundle_ShortVersionString];
            NSArray *cVersionArray=[currentVersion componentsSeparatedByString:@"."];
            int rVersion1=[versionArray.firstObject intValue];
            int rVersion2=versionArray.count>1?[versionArray[1] intValue]:0;
            int rVersion3=versionArray.count>2?[versionArray[2] intValue]:0;
            int cVersion1=[cVersionArray.firstObject intValue];
            int cVersion2=cVersionArray.count>1?[cVersionArray[1] intValue]:0;
            int cVersion3=cVersionArray.count>2?[cVersionArray[2] intValue]:0;
            BOOL isHasNewVersion=NO;
            if (rVersion1>cVersion1) {
                isHasNewVersion=YES;
            }else if(rVersion1==cVersion1&&rVersion2>cVersion2) {
                isHasNewVersion=YES;
            }else if(rVersion1==cVersion1&&rVersion2==cVersion2&&rVersion3>cVersion3){
                isHasNewVersion=YES;
            }else{
                isHasNewVersion=NO;
            }
            BOOL isForce=[data[@"forceUpdate"] isEqualToString:@"Y"];
            if(isHasNewVersion){
                if (isForce) {
                    RIButtonItem *okItem=[RIButtonItem itemWithLabel:@"更新" action:^{
                        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:iTunesURL]];
                    }];
                    UIAlertView *alertView=[[UIAlertView alloc] initWithTitle:@"更新提示" message:@"检测到新版本,您必须更新到最新版本才能继续使用!" cancelButtonItem:nil otherButtonItems:okItem, nil];
                    [alertView show];
                }else{
                    RIButtonItem *cancelItem=[RIButtonItem itemWithLabel:@"取消"];
                    RIButtonItem *okItem=[RIButtonItem itemWithLabel:@"更新" action:^{
                        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:iTunesURL]];
                    }];
                    UIAlertView *alertView=[[UIAlertView alloc] initWithTitle:@"有新版本是否更新?" message:@"" cancelButtonItem:cancelItem otherButtonItems:okItem, nil];
                    [alertView show];
                }
            }
        }
    } failure:^(NSDictionary *data) {
        if ([data isKindOfClass:[NSDictionary class]]) {
            if ([data[@"code"] intValue]==404) {
                [ServiceManager changeHostToQA];
                [self autoLogin];
            }
        }
    }];
}

@end
